﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_equip officer_equip(string officerprimarycode, string eqmprimarycode)
        {
            string userCode = SangoManager.Authentication();

            officer_equip response = ReflectionManager.CreatePojo<officer_equip>();
            response.Succeed = false;

            if (!Verification.Verifies(officerprimarycode, eqmprimarycode).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(officerprimarycode, eqmprimarycode);

            Entities.Usr_Equipment onEqm = EquipmentDao.tryGetUserNotBufferFragmentEqm(eqmprimarycode);

            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);

            if (onEqm == null)
            {
                response.Message = "装备不存在。";
                return response;
            }

            Entities.Eqm_Equipment sysEqm = EqmEquipmentHelper.searchBySerialcode(onEqm.Equipmentserialcode);

            Entities.Usr_Officer usrofficer = OfficerDao.getUserOfficer(userCode, officerprimarycode);

            if (usrofficer.Officerlevel < sysEqm.Requiredlevel)
            {
                response.Message = "将领等级不足";
                return response;
            }

            Entities.Usr_Equipment offEqm = null;

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                offEqm = EquipmentDao.tryGetOfficerEqm(userCode, officerprimarycode, onEqm.Equipmenttypecode);// 获取武将正在穿的装备

                if (offEqm != null)
                {
                    OfficerDao.eqmOff(usrofficer, offEqm, property);
                    command.Update(offEqm);
                }



                OfficerDao.eqmOn(usrofficer, onEqm, property);

                command.Update(usrofficer);

                command.Update(onEqm);

                command.Update(property);

                command.Commit();
            });

            response.property = UsrProfileFacade.ToProperty(property);
            if (onEqm != null)
                response.oneqm = EquipmentFacade.ToEquipment(onEqm, EquipmentDao.getUserEqmExtras(userCode, onEqm.Primarycode));
            if (offEqm != null)
                response.offeqm = EquipmentFacade.ToEquipment(offEqm, EquipmentDao.getUserEqmExtras(userCode, offEqm.Primarycode));
            response.Succeed = true;
            return response;
        }


    }
    public interface officer_equip : ISangoResponse
    {
        usr_equipment oneqm { get;set;}
        usr_equipment offeqm { get;set;}
        player_property property { get;set;}
    }
}
